package blog.com.mw.controller;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import blog.com.mw.base.ResultData;
import blog.com.mw.pojo.po.UserPO;
import blog.com.mw.pojo.vo.UserVO;
import blog.com.mw.service.UserService;

/**
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
@SessionAttributes(value = "currentUser",types=UserPO.class)
public class UserController {

	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/user",method =RequestMethod.GET)
	public ResultData getUserById(@RequestParam("id") int id) {
		ResultData rs = null;
		rs = userService.getUserById(id);
		return rs;
	}
	@RequestMapping(value = "/userList",method =RequestMethod.GET)
	public ResultData getAllUser(@RequestParam("page") int pageNum,@RequestParam("limit") int pageSize
			,@RequestParam(value="keyword", required=false)String keyword) {
		ResultData rs = null;
		rs = userService.getAllUser(pageNum,pageSize,keyword);
		return rs;
	}
	@RequestMapping(value = "/user",method =RequestMethod.PUT)
	public ResultData updateUser(@RequestBody UserVO user) {
		ResultData rs = null;
		rs = userService.updateUser(user);
		return rs;
	}
	
	@RequestMapping(value = "/user",method =RequestMethod.POST)
	public ResultData addUser(@RequestBody UserVO user) {
		ResultData rs = null;
		rs = userService.register(user);
		return rs;
	}
	
	@RequestMapping(value = "/user/{id}",method =RequestMethod.DELETE)
	public ResultData delUser(@PathVariable("id") int id) {
		ResultData rs = null;
		List<Integer> ids = new ArrayList<>();
		ids.add(id);
		rs = userService.delUser(ids);
		return rs;
	}
	
	@RequestMapping(value = "/user",method =RequestMethod.DELETE)
	public ResultData delUserByIds(@RequestBody List<Integer> ids) {
		ResultData rs = null;
		rs = userService.delUser(ids);
		return rs;
	}
	
	@RequestMapping(value = "/resetPassword/{id}",method =RequestMethod.PUT)
	public ResultData resetPassword(@PathVariable("id") int id) {
		ResultData rs = null;
		rs = userService.resetPwd(id);
		return rs;
	}
	
	@RequestMapping(value = "/register",method =RequestMethod.POST)
	public ResultData register(@RequestBody UserVO user) {
		ResultData rs = null;
		rs = userService.register(user);
		return rs;
	}
	
	@RequestMapping(value = "/login",method =RequestMethod.POST)
	public ResultData login (@RequestBody UserVO user,HttpServletRequest request) {
		ResultData rs = null;
		rs = userService.login(user);
		request.getSession().setAttribute("currentUser", (UserPO)rs.getData().get("data"));
		System.out.print(request.getSession().getAttribute("currentUser"));
		return rs;
	}
	
	@RequestMapping(value = "/getVeriCode",method =RequestMethod.GET)
	public ResultData getVeriCode (@RequestParam("email") String email) {
		ResultData rs = null;
		rs = userService.getVeriCode(email);
		return rs;
		
	}
	
}
